package com.wardrobe.soa.upload;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Calendar;
import java.util.UUID;

/**
 * Created by Administrator on 2018/11/15.
 */
@Controller
@RequestMapping("/api")
public class UploadFile {
    @Value("${image.file.path}")
    private String filePath;
    @Value("${image.file.url}")
    private String fileUrl;
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) {
        Calendar currTime = Calendar.getInstance();
        String time = String.valueOf(currTime.get(Calendar.YEAR))+String.valueOf((currTime.get(Calendar.MONTH)+1));
        String path =filePath+time;
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        suffix = suffix.toLowerCase();
        JSONObject result = new JSONObject();
        if(suffix.equals(".jpg") || suffix.equals(".jpeg") || suffix.equals(".png") || suffix.equals(".gif")){
            String fileName = UUID.randomUUID().toString()+suffix;
            File targetFile = new File(path, fileName);
            if(!targetFile.getParentFile().exists()){
                //注意，判断父级路径是否存在
                targetFile.getParentFile().mkdirs();
            }
            long size = 0;
            //保存
            try {
                file.transferTo(targetFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
            result.put("fileUrl",fileUrl+"/image/"+time+"/"+fileName);
            result.put("status", 1);
            System.out.println(result.toString());
            return result.toString();
        }else{
            result.put("error", "上传文件类型不支持!");
            result.put("status", 2);
            return result.toString();
        }

    }
    @RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
    @ResponseBody
    public String upload(HttpServletRequest request, HttpServletResponse response) {
       // 先实例化一个文件解析器
        CommonsMultipartResolver coMultipartResolver =new CommonsMultipartResolver(request.getSession().getServletContext());
        // 判断request请求中是否有文件上传
        JSONObject result = new JSONObject();
        if(coMultipartResolver.isMultipart(request)){
            // 转换request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            MultipartFile file = multiRequest.getFile("file");
            Calendar currTime = Calendar.getInstance();
            String time = String.valueOf(currTime.get(Calendar.YEAR))+String.valueOf((currTime.get(Calendar.MONTH)+1));
            String path =filePath+time;
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            suffix = suffix.toLowerCase();
            if(suffix.equals(".jpg") || suffix.equals(".jpeg") || suffix.equals(".png") || suffix.equals(".gif")){
                String fileName = UUID.randomUUID().toString()+suffix;
                File targetFile = new File(path, fileName);
                if(!targetFile.getParentFile().exists()){
                    //注意，判断父级路径是否存在
                    targetFile.getParentFile().mkdirs();
                }
                long size = 0;
                //保存
                try {
                    file.transferTo(targetFile);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                result.put("fileUrl",fileUrl+"/image/"+time+"/"+fileName);
                result.put("status", 1);
                return result.toString();
            }else{
                result.put("error", "上传文件类型不支持!");
                result.put("status", 2);
                return result.toString();
            }
        }
        result.put("error", "不含有文件上传!");
        result.put("status", 2);
        return result.toString();
    }
}
